package com.chen.cas;

import java.util.concurrent.atomic.AtomicInteger;

// compareAndSet 比较并交换
public class CASDemo {
    public static void main(String[] args) {

        AtomicInteger atomicInteger = new AtomicInteger(2020);

        // 如果是期望值，则进行更新，否则不更新
        System.out.println(atomicInteger.compareAndSet(2020, 2021));// true
        System.out.println(atomicInteger.get());// 2021

        System.out.println(atomicInteger.compareAndSet(2020, 2021));// false
        System.out.println(atomicInteger.get());// 2021

    }
}
